package com.example.jee_springboot.jpa.repository;

import com.example.jee_springboot.jpa.entity.Publisher;
import org.apache.ibatis.annotations.Param;
import org.springframework.data.jpa.repository.EntityGraph;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;

import javax.persistence.Entity;
import java.util.List;
import java.util.Optional;

public interface PublisherRepository extends JpaRepository<Publisher, Long> {

    public List<Publisher> findByAddress_City(String city);

    public List<Publisher> findByAddress_CityOrderByAddress_Province(String province);

    //具有惰性加载问题
    Optional<Publisher> findByInfo(String string);


    @Query("from Publisher p join fetch p.authors where p.info=:info")
    Optional<Publisher> solveByJoinFetch(String info);
    //解决了惰性加载问题
    @EntityGraph(attributePaths = "authors")
    Optional<Publisher> readByInfo(String string);

    //authors 一起加载 其他惰性加载
    @Query("from Publisher where info=:info")
    Optional<Publisher> getByInfoNoEntityGraph(@Param("info") String info);
    @EntityGraph(attributePaths = {"authors", "address"}, type = EntityGraph.EntityGraphType.FETCH)
    @Query("from Publisher where info=:info")
    Optional<Publisher> solveByFetch(@Param("info") String info);
    @EntityGraph(attributePaths = "authors", type = EntityGraph.EntityGraphType.LOAD)
    @Query("from Publisher where info=:info")
    Optional<Publisher> solveByLoad(@Param("info") String info);

    @EntityGraph(value = "publisher-authors-graph")
    Optional<Publisher> searchByInfo(String string);

    //根据书来查出版社  通过下划线实现引用
    List<Publisher> findByAuthors_Books_Name(String name);
    @EntityGraph(attributePaths = "authors")
    Optional<Publisher>queryById(long id);
}